<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
import type { OrderConfirm, OrderRest } from '@/types/Order';
import { getOrderApi, createOrderApi } from'@/apis/order'
import type { confirmOrder } from '@/types/ConfirmOrder';
import { onLoad } from '@dcloudio/uni-app'
import { useAddressStore } from'@/stores';

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 获取传递的参数
// 接收跳转数据
const query = defineProps<{
  skuId: number,
  count: number,
  type: number,
}>()


// 订单备注
const buyerMessage = ref('')

const orderData = ref<OrderConfirm>()
const getOrderPreData = async(data : confirmOrder) => {
  const res = await getOrderApi(data)
  orderData.value = res.data
}

// 计算默认地址
const addressStore = useAddressStore()
const selectAddress = computed(() => {
  return addressStore.selectAddress || orderData.value?.address.find(v => v.defaultStatus===0)
})

// 提交订单
const createOrder = async() => {
  // 没有收货地址提醒
  if (!selectAddress.value?.id) {
    return uni.showToast({ icon: 'none', title: '请选择收货地址' })
  }
  const res = await createOrderApi({
    addressId:selectAddress.value?.id,
    note:buyerMessage.value,
    // skuIds:orderData.value!.orderItem.map((v) => v.skuId),
    token:orderData.value!.token,
    payType:1,
    sourceType:2,
    type:query.type,
    count:query.count,
    skuId:query.skuId,
  })
  // 成功跳转
  if(res.code === 0){
    uni.redirectTo({url:`/pagesOrder/detail/detail?id=${res.data}`})
  }
}

const form = ref<confirmOrder>({
  skuId:query.skuId,
  count:query.count,
  type:query.type,
})

onLoad(() => {
  getOrderPreData(form.value)
})
</script>

<template>
  <scroll-view scroll-y class="viewport">
    <!-- 收货地址 -->
    <navigator
      v-if="selectAddress"
      class="shipment"
      hover-class="none"
      url="/pagesMember/address/address?from=order"
    >
      <view class="dizhi icon-dizhi"></view>
      <view class="user"> {{ selectAddress.name }} {{ selectAddress.phone }} </view>
      <view class="address"> {{ selectAddress.province }} {{ selectAddress.city }} {{ selectAddress.region }} {{ selectAddress.detailAddress }} </view>
      <text class="icon icon-youjiantou1"></text>
    </navigator>
    <navigator
      v-else
      class="shipment"
      hover-class="none"
      url="/pagesMember/address/address?from=order"
    >
      <view class="address"> 请选择收货地址 </view>
      <text class="icon icon-youjiantou1"></text>
    </navigator>

    <!-- 商品信息 -->
    <view class="goods">
      <navigator
        v-for="item in orderData?.orderItem"
        :key="item.skuId"
        :url="`/pages/goods/goods?skuId=${item.skuId}`"
        class="item"
        hover-class="none"
      >
        <image
          class="picture"
          :src="item.skuDefaultImg"
        />
        <view class="meta">
          <view class="name ellipsis">{{ item.skuTitle }}</view>
          <view class="attrs">{{ item.attribute }}</view>
          <view class="prices">
            <view class="pay-price symbol">{{ item.price.toFixed(2) }}</view>
          </view>
          <view class="count">×{{ item.count }}</view>
        </view>
      </navigator>
    </view>

    <!-- 支付金额 -->
    <view class="settlement">
      <view class="item">
        <text class="text">商品总价: </text>
        <text class="number symbol">{{ (orderData?.totalPrice?orderData?.totalPrice:0).toFixed(2) }}</text>
      </view>
      <view class="item">
        <text class="text">促销: </text>
        <view>
          <text class="danger">-</text><text class="number symbol danger">{{ ((orderData?.totalPrice?orderData?.totalPrice:0) - (orderData?.payPrice?orderData?.payPrice:0)).toFixed(2) }}</text>
        </view>
      </view>
      <!-- <view class="item">
        <text class="text">运费: </text>
        <view>
          <text class="danger">-</text><text class="number symbol danger"></text>
        </view>
      </view> -->
    </view>

    <!-- 配送及支付方式 -->
    <view class="related">
      <view class="item">
        <text class="text">订单备注</text>
        <input
          class="input"
          :cursor-spacing="30"
          placeholder="备注"
          v-model="buyerMessage"
        />
      </view>
    </view>
  </scroll-view>

  <!-- 吸底工具栏 -->
  <view class="toolbar" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
    <view class="total-pay symbol">
      <text class="number">{{ (orderData?.payPrice?orderData?.payPrice:0).toFixed(2) }}</text>
    </view>
    <view class="button" :class="{ disabled: !selectAddress?.id }" @tap="createOrder"> 提交订单 </view>
  </view>
</template>



<style lang="scss">
page {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.symbol::before {
  content: '¥';
  font-size: 80%;
  margin-right: 5rpx;
}

.shipment {
  margin: 20rpx;
  padding: 30rpx 30rpx 30rpx 84rpx;
  font-size: 26rpx;
  border-radius: 10rpx;
  position: relative;
  background: #fff;
  
  .dizhi{
    font-size: 36rpx;
    color: rgb(252, 74, 24);
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    left: 20rpx;
  }

  .icon {
    font-size: 36rpx;
    color: #333;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 20rpx;
  }

  .user {
    color: #333;
    margin-bottom: 10rpx;
  }

  .address {
    color: #666;
  }
}

.goods {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    padding: 30rpx 0;
    border-top: 1rpx solid #eee;

    &:first-child {
      border-top: none;
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
      border-radius: 10rpx;
      margin-right: 20rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: relative;
    }

    .name {
      height: 80rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrs {
      line-height: 1.8;
      padding: 0 15rpx;
      margin-top: 6rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .prices {
      display: flex;
      align-items: baseline;
      margin-top: 6rpx;
      font-size: 28rpx;

      .pay-price {
        margin-right: 10rpx;
        color: #cf4444;
      }

      .price {
        font-size: 24rpx;
        color: #999;
        text-decoration: line-through;
      }
    }

    .count {
      position: absolute;
      bottom: 0;
      right: 0;
      font-size: 26rpx;
      color: #444;
    }
  }
}

.related {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .input {
    flex: 1;
    text-align: right;
    margin: 20rpx 0;
    padding-right: 20rpx;
    font-size: 26rpx;
    color: #999;
  }

  .item .text {
    width: 125rpx;
  }

  .picker {
    color: #666;
  }

  .picker::after {
    content: '\e6c2';
  }
}

/* 结算清单 */
.settlement {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .danger {
    color: #cf4444;
  }
}

/* 吸底工具栏 */
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--window-bottom));
  z-index: 1;

  background-color: #fff;
  height: 100rpx;
  padding: 0 20rpx;
  border-top: 1rpx solid #eaeaea;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: content-box;

  .total-pay {
    font-size: 40rpx;
    color: #cf4444;

    .decimal {
      font-size: 75%;
    }
  }

  .button {
    width: 220rpx;
    text-align: center;
    line-height: 72rpx;
    font-size: 26rpx;
    color: #fff;
    border-radius: 72rpx;
    background-color: rgb(246, 19, 19);
  }

  .disabled {
    opacity: 0.6;
  }
}
</style>